//
//  FirstController.m
//  i优购商城
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "FirstController.h"
#import "HttpRequestManager.h"
#import "Product.h"
#import "HeaderView.h"
#import "UIImageView+AFNetworking.h"
#import "FourCell.h"
#import "HeadView.h"

#import "longCell.h"
@interface FirstController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
    NSMutableArray*_dataArry;
    NSMutableArray*_headerArry;
    UIPageControl *_pageControl;
    HeaderView*_header;
    NSString*_str;
    int _count;
}
@property (weak, nonatomic) IBOutlet UITableView *myTableView;



@end

@implementation FirstController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _dataArry=[[NSMutableArray alloc]init];
        _headerArry=[[NSMutableArray alloc]init];
        _count=1;
        
     
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadDataFromNet];
    [self registerCell];
   

   
}

-(void)loadDataFromNet
{
    [[HttpRequestManager shareInstance]getHomeDataSuccess:^(id responseobject) {
        [_dataArry addObjectsFromArray:responseobject[@"homeview"]];
        NSArray*arry=responseobject[@"home_banner"];
        for (NSDictionary*dic in arry) {
            Product*item=[[Product alloc]init];
            item.pic=dic[@"pic"];
            item.title=dic[@"title"];
            item.typeId=dic[@"type_argu"];
            [_headerArry addObject:item];
        }
        _str=responseobject[@"home_hotlist"][0][@"title"];

        //定义表头
        _header=[[NSBundle mainBundle]loadNibNamed:@"HeaderView" owner:self options:nil][0];
        _header.titleLbl.text=_str;
        _header.mySrcoller.delegate=self;
        _header.titleLbl.adjustsFontSizeToFitWidth=YES;
        _header.mySrcoller.contentSize=CGSizeMake(_header.mySrcoller.frame.size.width*8, 0);
        for (int i=0; i<8; i++) {
            UIImageView*imgView=[[UIImageView alloc]initWithFrame:CGRectMake(_header.mySrcoller.frame.size.width*i, 0, _header.mySrcoller.frame.size.width, 180)];
            if (i==0) {
                Product*item=_headerArry[5];
                [imgView setImageWithURL:[NSURL URLWithString:item.pic]];
                
            }
            else if(i==7)
            {
                Product*item=_headerArry[0];
                [imgView setImageWithURL:[NSURL URLWithString:item.pic]];
            }
            else
            {
            
            Product*item=_headerArry[i-1];
            [imgView setImageWithURL:[NSURL URLWithString:item.pic]];
            [_header.mySrcoller addSubview:imgView];
            }
        }
        //默认显示第二张图
        _header.mySrcoller.contentOffset=CGPointMake(_header.mySrcoller.frame.size.width, 0);
        
        //定义计时器和 圆点控制器
        UIView*boomView=[[UIView alloc]initWithFrame:CGRectMake(0, _header.mySrcoller.bounds.size.height-20, _header.mySrcoller.bounds.size.width, 20)];
        boomView.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.5];
        [_header addSubview:boomView];
        _pageControl=[[UIPageControl alloc]initWithFrame:boomView.bounds];
        _pageControl.numberOfPages=_headerArry.count;
        _pageControl.currentPageIndicatorTintColor=[UIColor lightGrayColor];
        _pageControl.pageIndicatorTintColor=[UIColor whiteColor];
        [_pageControl addTarget:self action:@selector(pageControlValueChanged) forControlEvents:UIControlEventValueChanged];
        [boomView addSubview:_pageControl];
        _myTableView.tableHeaderView=_header;
        
        //定义表头其他的图片
        NSMutableArray*arry1=[[NSMutableArray alloc]init];
        NSDictionary*dic=responseobject[@"Home_topic"][0];
        [arry1 addObject:dic];
        dic=responseobject[@"Home_topic"][1];
        [arry1 addObject:dic];
        [_header.img1View setImageWithURL:[NSURL URLWithString:arry1[0][@"pic"]]];
        [_header.img2View setImageWithURL:[NSURL URLWithString:arry1[1][@"pic"]]];
        [_header.img3View setImageWithURL:[NSURL URLWithString:responseobject[@"shake"][@"image_url"]]];
       
        [_myTableView reloadData];
        [self initTimer];
    } Failure:^(NSError *error) {
        NSLog(@"请求数据失败=== %@",error);
    }];
    
}
//注册Cell
-(void)registerCell
{

    [_myTableView registerNib:[UINib nibWithNibName:@"FourCell" bundle:nil] forCellReuseIdentifier:@"FourCell"];
    [_myTableView registerNib:[UINib nibWithNibName:@"longCell" bundle:nil] forCellReuseIdentifier:@"longCell"];

}

//定时器
-(void)initTimer
{
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
    
}
//定时器的执行方法
-(void)timerChange
{
    _count++;
    if (_count>=7) {
        _count=1;
        _header.mySrcoller.contentOffset=CGPointMake(0, 0);
    }
    [_header.mySrcoller setContentOffset:CGPointMake(_count*_header.mySrcoller.frame.size.width, 0) animated:YES];
    _pageControl.currentPage=_count-1;
    

}


#pragma pageControll  的点击事件

-(void)pageControlValueChanged
{
    [_header.mySrcoller setContentOffset:CGPointMake((_pageControl.currentPage+1)*_header.mySrcoller.bounds.size.width, 0)animated:YES];
}

#pragma UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int currIndex=_header.mySrcoller.contentOffset.x/_header.mySrcoller.bounds.size.width;
    _pageControl.currentPage=currIndex-1;
    if (currIndex==7) {
        _header.mySrcoller.contentOffset=CGPointMake(_header.mySrcoller.frame.size.width, 0);
        _pageControl.currentPage=0;
        
    }
    if (currIndex==0) {
        _header.mySrcoller.contentOffset=CGPointMake(_header.mySrcoller.frame.size.width*6, 0);
        _pageControl.currentPage=5;
    }
   
}


#pragma mark-UITableViewDataSource&UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }
    if (section==5||section==6||section==11) {
        return 2;
    }
    NSArray*arry=_dataArry[section][@"home_details"];
    return arry.count;
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    NSMutableArray *arry=[[NSMutableArray alloc]init];
    arry=_dataArry[indexPath.section][@"home_details"];
     NSDictionary*dic=arry[indexPath.row];
    if (indexPath.section==0) {
        FourCell*cell=[tableView dequeueReusableCellWithIdentifier:@"FourCell" forIndexPath:indexPath];
        cell.dataArry=arry;
        [cell.myCollectionView reloadData];
        return cell;
   
    }
    if (indexPath.section==5||indexPath.section==6||indexPath.section==11) {
        
       
        if (indexPath.row==0) {
            longCell*cell=[tableView dequeueReusableCellWithIdentifier:@"longCell" forIndexPath:indexPath];
            [cell.imgView setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
            return cell;
        }
        
        if (indexPath.row==1 )
        {
            FourCell*cell=[tableView dequeueReusableCellWithIdentifier:@"FourCell" forIndexPath:indexPath];
            NSMutableArray*arry1=[[NSMutableArray alloc]init];
            [arry1 addObjectsFromArray:arry];
            [arry1 removeObjectAtIndex:0];
            cell.dataArry=arry1;
            [cell.myCollectionView reloadData];
            return cell;
        
        }
        
       
        
    }
    
    
    longCell*cell=[tableView dequeueReusableCellWithIdentifier:@"longCell" forIndexPath:indexPath];
    [cell.imgView setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
    return cell;
    // 
    
   
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary*dic=_dataArry[section];
    NSString*Str=dic[@"home_title"];
    if (Str.length>0) {
        HeadView*head=[[NSBundle mainBundle]loadNibNamed:@"HeadView" owner:self     options:nil][0];
        head.lab.text=Str;
        return head;
    }
    UIView*view1=[[UIView alloc]init];
    return view1;
    
}





-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSDictionary*dic=_dataArry[section];
    NSString*Str=dic[@"home_title"];
    if (Str.length>0) {
        return 20;
    }
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 200;
    }
    
    return 100;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00000001;
}

@end
